﻿'========================================================================
'功能:
'作者:andy
'日期: 
'========================================================================
Imports System.Data
Imports System.IO
Partial Class WebForm_Mail_Reply
    Inherits BasePage
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        CType(Me.Master.FindControl("smBase"), ScriptManager).RegisterPostBackControl(btnPrint)
        If Not IsPostBack Then
            hiUID.Value = ActualUID
            hiDBName.Value = dbName
            txtOwner.Text = ActualUID
            txtReturnDeadLin1.Text = Today.ToString("yyyy/MM/01")
            txtReturnDeadLin2.Text = DateTime.Parse(Today.AddMonths(1).ToString("yyyy/MM/01")).AddDays(-1).ToString("yyyy/MM/dd")






        End If

    End Sub
   
    Protected Sub btnDetail_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Me.AssetDetail1.InitDataAndShow(CType(sender, LinkButton).CommandArgument.ToString())
    End Sub

    Protected Sub btnQuery_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnQuery.Click
        Try
            gvMain.Visible = False
            DateTime.Parse(txtReturnDeadLin1.Text)
            DateTime.Parse(txtReturnDeadLin2.Text)

        Catch ex As Exception
            ShowMessageForAjax(btnPrint, "Reply deadline Date is wrong.")
            Exit Sub
        End Try
        gvMain.Visible = True
        gvMain.DataBind()
    End Sub

    Protected Sub btnPrint_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrint.Click
        Dim bbl As AssetManage.BBL.AssetBBL = New AssetManage.BBL.AssetBBL()

        Dim dt As DataTable = bbl.GetUnReplyData(ddlAssetDef.SelectedValue, txtOwner.Text, txtasset_serialNo.Text, txtDept.Text, ddlHouseID.SelectedValue, DateTime.Parse(txtReturnDeadLin1.Text), DateTime.Parse(txtReturnDeadLin2.Text), ddlSendMail.SelectedValue, hiDBName.Value)
        Dim dt1 As DataTable = New DataTable
        Dim dr As DataRow
        dt1.Columns.Add(GetLocalResourceObject("colAssetDef.HeaderText"))
        dt1.Columns.Add(GetLocalResourceObject("colAsset_SerialNo.HeaderText"))
        dt1.Columns.Add(GetLocalResourceObject("colAssetowner.HeaderText"))
        dt1.Columns.Add("OwnerName")
        dt1.Columns.Add(GetLocalResourceObject("colUser1.HeaderText"))
        dt1.Columns.Add(GetLocalResourceObject("colStatus.HeaderText"))
        dt1.Columns.Add(GetLocalResourceObject("colComputerName.HeaderText"))
        dt1.Columns.Add("Class")
        dt1.Columns.Add(GetLocalResourceObject("colLoacation.HeaderText"))
        dt1.Columns.Add(GetLocalResourceObject("colReturnDeadLine.HeaderText"))

        For ii As Integer = 0 To dt.Rows.Count - 1
            dr = dt1.NewRow()
            dr(0) = dt.Rows(ii)("DefName")
            dr(1) = dt.Rows(ii)("asset_serialNO")
            dr(2) = dt.Rows(ii)("asset_owner")
            dr(3) = dt.Rows(ii)("OwnerName").ToString()

            dr(4) = dt.Rows(ii)("User1")
            dr(5) = dt.Rows(ii)("asset_status")
            dr(6) = dt.Rows(ii)("ComputerName")
            dr(7) = dt.Rows(ii)("Class")
            dr(8) = dt.Rows(ii)("UsingType")
            Try
                dr(9) = DateTime.Parse(dt.Rows(ii)("ReturnDeadLine")).ToString("yyyy/MM/dd")
            Catch ex As Exception
                dr(9) = ""
            End Try


            dt1.Rows.Add(dr)
        Next


        If ddlExportKind.SelectedValue = "2" Then



            Try
                OOoTools.ConnectOpenOffice()
                ' Server.CreateObject("com.sun.star.ServiceManager")
                ''  HelloWorldExample()
                Dim path = Now.ToString("yyyyMMddHHmmssffff") + ".ods"
                OOo_Examples.CalcExportDataTable(Server.MapPath("~/") & "Imports\" + path, dt1)
                OOoTools.DisconnectOpenOffice()
                Response.Redirect(Server.MapPath("~/") & "Imports\" + path)

            Catch ex As Exception
            Finally


            End Try
        Else


            Try
                Dim ms As MemoryStream = TryCast(DataTableRenderToExcel.RenderDataTableToExcel(dt1), MemoryStream)
                ' 設定強制下載標頭。
                Response.AddHeader("Content-Disposition", String.Format("attachment; filename={0}.xls", Server.UrlEncode("ReplyStatistics")))
                ' 輸出檔案。
                Response.BinaryWrite(ms.ToArray())

                ms.Close()
                ms.Dispose()
            Catch ex As Exception

            End Try

        End If
    End Sub

  
    Protected Sub ddlSendMail_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlSendMail.DataBound
        ddlSendMail.Items.Insert(0, New ListItem("", ""))
    End Sub
End Class
